package gui;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.util.Hashtable;

import javax.swing.event.*;

import physics.PhysicsEngine;
import physics.Vector;

public class SettingsWindow extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SettingsWindow thiz = this;
	private final PhysicsSettings s;
	private NumberInput scale;
	private BooleanCheckBox showForces;
	private Hashtable<Vector.Type,ColorButton> colors;
	private BooleanDropDown showCollision;
	public SettingsWindow(final PhysicsSettings s) {
		this.s = s;
		setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
		scale = new NumberInput("Scale",100/s.getScale());
		add(scale);
		showCollision = new BooleanDropDown(PhysicsEngine.getPhysicsEngine().isStableCode(), "Stable Collisions", "Stable", "Unstable");
		add(showCollision);
		showForces = new BooleanCheckBox("Show Forces",s.getShowForces());
		add(showForces);
		colors = new Hashtable<Vector.Type,ColorButton>();
		for(Vector.Type t : Vector.Type.values()) {
			colors.put(t, new ColorButton(t.toString(),s.getColor(t)));
			add(colors.get(t));
		}
		JButton ok = new JButton("Ok");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				s.setScale(100/scale.getNum());
				s.setShowForces(showForces.getValue());
				s.setStableCollisions(showCollision.getValue());
				for(Vector.Type t : Vector.Type.values()) {
					s.setColor(t, colors.get(t).getColor());
				}
				WindowEvent wev = new WindowEvent(thiz, WindowEvent.WINDOW_CLOSING);
                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
			}
		});
		add(ok);
	}
}
